<?php
/**
* @name church.php
* @copyright (c) 2011
* @author Arsene Tochemey GANDOTE<arsene.gandote@yahoo.com>
*/
class Organisation extends AppModel {
	# Model name
	var $name = 'Organisation';
	# Display field
	var $displayField = 'name';
	# validation rules
	var $validates = array(
          # Registration is not null and it is required and must be alphanumeric and unique.
          /** 'registration_number' => array(
              # definition of not null rule
              'notNull' =>array(
                  'rule' => 'notEmpty',
                  'required' => true,
                  'message' => 'The Registration Number must not be null and it is required',
                  'last' => true
              ),
              # definition of alphanumeric rule
              'alphanumeric' => array(
                'rule' => ' /^([A-Za-z0-9]+)\s*|([a-zA-Z0-9]*)$/',
                'required' => true,
                'message' => 'Registration Number must alphanumeric: Only Letters and numbers are allowed',
                'last' => true
              ),
              # definition of unique rule
              'unique' => array(
                'rule' => 'isUnique',
                'message' => 'This Registration Number has already been recorded',
                'last' => true
              )
          ),*/
          # Church Name is not null and it is required and must be alphabectic.
          'name' => array(
              # definition of not null rule
              'notNull' =>array(
                  'rule' => 'notEmpty',
                  'required' => true,
                  'message' => 'The Name must not be null and it is required',
                  'last' => true
              ),
              # definition of alphabetic rule
              'alphabetic' => array(
                'rule' => ' /^([A-Za-z]+)\s*|([a-zA-Z]*)$/',
                'required' => true,
                'message' => 'The Name must be alphabetic: Only Letters are allowed',
                'last' => true
              )
          ),
         /** # Vision is not null and it is required and must be alphabetic
          'vision' => array(
              # definition of not null rule
              'notNull' =>array(
                  'rule' => 'notEmpty',
                  'required' => true,
                  'message' => 'The Church Vision must not be null and it is required',
                  'last' => true
              ),
              # definition of alphabetic rule
              'alphabetic' => array(
                'rule' => ' /^([A-Za-z]+)\s*|([a-zA-Z]*)$/',
                'required' => true,
                'message' => 'Church Vision must be alphabetic: Only Letters are allowed',
                'last' => true
              )
          ),
          # Missions are not null and are required
          'missions' => array(
              'rule' => 'notEmpty',
              'required' => true,
              'message' => 'The Church Missions must not be null and are required'
          ),*/
          # Date of Establishment is not null and it is required and must be in dat format
          'date_of_establishment' => array(
              # definition of not null rule
              /** 'notNull' =>array(
                  'rule' => 'notEmpty',
                  'required' => true,
                  'message' => 'Date of Establishment must not be null and it is required',
                  'last' => true
              ),*/
              # definition of dat format rule
              'dateFormat' => array(
                'rule' => array('date', 'ymd'),
                'required' => true,
                'message' => 'Date of Establishment is not in valid Date format. The Format is YYYY-MM-DD',
                'last' => true
              )
          ),
          # City is not null and it is required and must alphanumeric
          'city' => array(
              # definition of not null rule
              'notNull' =>array(
                  'rule' => 'notEmpty',
                  'required' => true,
                  'message' => 'City of location must not be null and it is required',
                  'last' => true
              ),
              # definition of alphanumeric rule
              'alphanumeric' => array(
                'rule' => ' /^([A-Za-z0-9]+)\s*|([a-zA-Z0-9]*)$/',
                'required' => true,
                'message' => 'City of location must be alphanumeric: Only Letters and Numbers are allowed',
                'last' => true
              )
          ),
          # Country is not null and it is required and must be alphanumeric
          'country' => array(
              # definition of not null rule
              'notNull' =>array(
                  'rule' => 'notEmpty',
                  'required' => true,
                  'message' => 'Country of location must not be null and it is required',
                  'last' => true
              ),
              # definition of alphanumeric rule
              'alphanumeric' => array(
                'rule' => ' /^([A-Za-z0-9]+)\s*|([a-zA-Z0-9]*)$/',
                'required' => true,
                'message' => 'Country of location must be alphanumeric: Only Letters and Numbers are allowed',
                'last' => true
              )
          ),
          # Zip code can be null but when defined must be a valid Postal code.
        /** 'zipcode' => array(
             'rule' => array('postal', null, 'uk'),
             'allowEmpty' => true,
             'message' => 'ZipCode is not in a valid postal code format. The Format used is the UK format.'
         ),*/
          # Location is not null and it is required and must be alphanumeric
          'location' => array(
              # definition of not null rule
              'notNull' =>array(
                  'rule' => 'notEmpty',
                  'required' => true,
                  'message' => 'location must not be null and it is required',
                  'last' => true
              ),
              # definition of alphanumeric rule
              'alphanumeric' => array(
                'rule' => ' /^([A-Za-z0-9]+)\s*|([a-zA-Z0-9]*)$/',
                'required' => true,
                'message' => 'location must be alphanumeric: Only Letters and Numbers are allowed',
                'last' => true
              )
          ),
          # Street can be null but when defined must be alphanumeric
          'street_number' => array(
              'rule' => ' /^([A-Za-z0-9]+)\s*|([a-zA-Z0-9]*)$/',
              'allowEmpty' => true,
              'message' => 'location street number must be alphanumeric: Only Letters and Numbers are allowed'
          ),
          # Telephone 1 is not null and it is required and must in a valid telephone format
          'telephone_1' => array(
              # definition of not null rule
              'notNull' =>array(
                  'rule' => 'notEmpty',
                  'required' => true,
                  'message' => 'Main Telpehone Number must not be null and it is required',
                  'last' => true
              ),
              # definition of valid telephone number rule
              'telephoneFormat' => array(
                'rule' => array('phone',  '/^(\+\d+)?( |\-)?(\(?\d+\)?)?( |\-)?(\d+( |\-)?)*\d+$/'),
                'required' => true,
                'message' => 'Main Telpehone Number is not in a valid telephone number format',
                'last' => true
              )
          ),
          # Telephone 2 can be null but when defined must be in a valid telephone format
          'telephone_2' => array(
              'rule' =>array('phone',  '/^(\+\d+)?( |\-)?(\(?\d+\)?)?( |\-)?(\d+( |\-)?)*\d+$/'),
              'allowEmpty' => true,
              'message' => 'Other Telpehone Number is not in a valid telephone number format'
          ),
          # Fax numbercan be null but when defined must be in valid fax format
          'fax_number' => array(
              'rule' =>array('phone',  '/^(\+\d+)?( |\-)?(\(?\d+\)?)?( |\-)?(\d+( |\-)?)*\d+$/'),
              'allowEmpty' => true,
              'message' => 'Fax Number is not in a valid fax number format'
          ),
          # Email 1 is not null and it is required and must be in a valid email format.
          'email_1' => array(
              # definition of not null rule
              'notNull' =>array(
                  'rule' => 'notEmpty',
                  'required' => true,
                  'message' => 'The Main Email Address must not be null and it is required',
                  'last' => true
              ),
              # definition of valid email rule
              'emailFormat' => array(
                'rule' => ' /^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/',
                'required' => true,
                'message' => 'Main Email Address is not in a valid email format',
                'last' => true
              )
          ),
          #  Email 2 can be null but when defined must in a valid email format
          'email_2' => array(
              'rule' => ' /^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/',
              'allowEmpty' => true,
              'message' => 'Other Email  is not in a valid email format'
          ),
          # Postal Box is not null and it is required.
          'postal_box' => array(
              # definition of not null rule
              'notNull' =>array(
                  'rule' => 'notEmpty',
                  'required' => true,
                  'message' => 'The Postal Box must not be null and it is required',
                  'last' => true
              ),
              # definition of alphanumeric rule
              'alphanumeric' => array(
                'rule' => ' /^([A-Za-z0-9]+)\s*|([a-zA-Z0-9]*)$/',
                'required' => true,
                'message' => 'Postal Box must be alphanumeric: Only Letters and numbers are allowed',
                'last' => true
              )
          )
	);

	# Methods to interact with the database table churches via the model Church
	/**
	* This function is used to retrieve the church data from the database table churches
	* @name getChurchData
	* @param void
	* @return Array containing the church data
	* @access public
	*/
	function getData(){
		# fetch the church data and return the data
		return $this->find('all');
	}

	/**
	* This function retrieves the church name from the database table churches
	* @name getChurchName
	* @param void
	* @var Array $fields Array containing the fields to retrieve from the database table churches.
	* @var Array $result Array containing the resultset of the query sent to the database.
	* @return string containing the church name to retrieve.
	* @access public
	*/
	function getName(){
		# field to retrieve
		$fields = array('name');
		# retrieve the field and return the data
		$result = $this->find('first', array(
			'fields' => $fields
		));
		return $result['Organisation']['name'];
	}


    function getOrgInfo(){
        # retrieve the field and return the data
        $result = $this->find('first', array(
            'recursive' => -1
        ));
        return $result['Organisation'];
    }
}
?>
